package leetcode.day_2020_2021_1_25_2021_1_31;

import java.util.ArrayList;
import java.util.List;

/**
 * 6. Z 字形变换
 * https://leetcode-cn.com/problems/zigzag-conversion/
 */
public class LeetCode0006 {
    public static void main(String[] args) {
        System.out.println(convert("PAYPALISHIRING", 3));
    }

    public static String convert(String s, int numRows) {
        if (numRows < 2) return s;
        List<StringBuilder> list = new ArrayList<>();
        for(int i=0; i<numRows; i++) list.add(new StringBuilder());
        int i=0; int flag = -1;
        for(char c : s.toCharArray()) {
            list.get(i).append(c);
            if(i==0 || i==numRows-1) flag=-flag;
            i += flag;
        }
        StringBuilder str = new StringBuilder();
        for(StringBuilder demo : list) {
            str.append(demo);
        }
        return str.toString();
    }
}
